/**
 * @description 用户信息、登录、退出登录
 */
import { createStore } from 'vuex';
import api from '@/app/admin/api';
import { getRouter } from '@/lite/utils/router'
import router from '@/lite/router';
import appStore from "@/app/admin/store/app";

export default createStore({
  namespaced: true,
  state: () => ({
    menu: [],
    permissions: [], // 菜单权限
    routes: [], // 路由
    userInfo: {},// 用户信息

  }),
  getters: {
    getRoutes(state) {
      return state.routes
    }
  },
  actions: {
    /**
     * @description 登录
     * @param {*} formData
     */
    async login(formData) {
      return await admin.account.login(formData);
    },

    /**
     * @description 设置token
     * @param {*} token
     */
    setToken(token = '') {
      this.token = token;
      if (token == '') {
        this.isLogin = false;
        $storage.remove('token');
      } else {
        this.isLogin = true;
        $storage.set('token', token);
      }
    },

    /**
     * @description 获取用户信息
     */
    async profile(context) {
      const { error, data } = await api.admin.profile();

      // 保存菜单

      // context.menu = getRouter(data.permission);
      // console.log(99,context.menu)

      // 设置路由
      context.state.routes = getRouter(data.permission)
      // 用户信息
      context.state.userInfo = data.user

      context.dispatch('setMenu', data.permission)

      
      return data;
    },
    setMenu(context) {
   

      const routers = context.state.routes
      const systemMenu = [
        {
          name: 'Dashboard',
          path: '/dashboard',
          children: [],
          component: '',
          meta: {
            title: '',
            icon:'MilkTea',
          }
        },
        // {
        //   name: '首页',
        //   path: '/',
        //   meta: {
        //     icon: 'Basketball',
        //     name: 'jdj'
        //   },
        //   children: [
        //     {
        //       name: '用户资料',
        //       path: '/profile',
        //       children: [
  
        //       ],
        //       component: '',
        //       meta: {
        //         title: 'jjd',
        //         icon: 'User',
  
  
        //       }
        //     },
        //     {
        //       name: 'Dashboard',
        //       path: '/dashboard',
        //       children: [
               
        //       ],
        //       component: '',
        //       meta: {
        //         title: 'jjd',
        //         icon:'MilkTea',
  
        //       }
        //     },
  
        //   ],
        //   component: '',
  
        // }
      ]
      routers.unshift(...systemMenu)
      routers.map(item => router.addRoute('layout', item))
    },

    /**
     * @description 获取用户菜单权限规则
     */
    async getRules() {
      const { error, data } = await admin.account.rules();
      if (error === 0) {
        this.menuRules = data.menu; // 设置菜单规则
        this.pageRules = data.permission; // 设置页面权限
        this.apiRules = data.permission; // 设置功能权限
        return data;
      }
    },

    /**
     * @description 退出登录
     * @param force
     */
    async logout(force = false) {
      let that = this;
      if (!force) {
        await admin.account.logout();
      }
      setTimeout(function () {
        that.info = {};
        that.setToken();
        that.setRules({ menu: [], page: [], api: [] });
        app().setTaskbar({ list: [], history: [] });
        window.location = location.pathname;
      }, 0);
    },

    /**
     * @description 设置菜单规则
     * @param payload
     */
    setRules(payload) {
      this.menuRules = payload.menu; // 设置菜单规则
      this.pageRules = payload.permission; // 设置页面权限
      this.apiRules = payload.permission; // 设置功能权限
    },
  },
})
